﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class BookingDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblBooking> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblBookings where a.Visible.Value select a;
                return list.ToList<tblBooking>();
            }
            catch
            {
                return new List<tblBooking>();
            }
        }

        public tblBooking GetByKey(int id)
        {
            try
            {
                var obj = dbContext.tblBookings.SingleOrDefault(a => a.Id == id && a.Visible.Value);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public int Insert(tblBooking obj)
        {
            try
            {
                dbContext.tblBookings.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                return obj.Id;
            }
            catch
            {
                return -1;
            }
        }

        public bool Update(tblBooking obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblBookings.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }

        public bool Delete(int id)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblBookings.SingleOrDefault(a => a.Id == id);
                if (exist != null)
                {
                    exist.Visible = false;
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion

        public tblBooking GetByBookingCode(string code)
        {
            try
            {
                var obj = dbContext.tblBookings.SingleOrDefault(a => a.BookingCode == code && a.Visible.Value);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public void UpdateBookingCode(int bookingId, string code)
        {
            try
            {
                var exist = dbContext.tblBookings.SingleOrDefault(a => a.Id == bookingId);
                if (exist != null)
                {
                    exist.BookingCode = code;
                    dbContext.SubmitChanges();
                }
            }
            catch
            {
                
            }
        }

        public void UpdateBookingStatus(int bookingId, string bookingStatus)
        {
            try
            {
                var exist = dbContext.tblBookings.SingleOrDefault(a => a.Id == bookingId);
                if (exist != null)
                {
                    exist.BookingStatus = bookingStatus;
                    dbContext.SubmitChanges();
                }
            }
            catch
            {
                
            }
        }
    }
}
